package com.pmcc.platform.user.service;


import com.pmcc.platform.giftGoods.entity.Goods;
import com.pmcc.platform.giftGoods.repository.GoodsRepository;
import com.pmcc.platform.shop.entity.ShopInfo;
import com.pmcc.platform.shop.repository.ShopInfoRepository;
import com.pmcc.platform.shopping.entity.GoodsGift;
import com.pmcc.platform.shopping.entity.Shopping;
import com.pmcc.platform.shopping.repository.GoodsGiftRepository;
import com.pmcc.platform.shopping.repository.ShoppingRepository;
import com.pmcc.platform.user.entity.UserInfo;
import com.pmcc.platform.user.repository.UserInfoRepository;
import com.pmcc.platform.user.vo.UserGetLogVo;
import com.pmcc.utils.AntPage;
import com.pmcc.utils.ApiResultBean;
import com.pmcc.utils.StringUtil;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class UserService {

    @Autowired
    UserInfoRepository userInfoRepository;

    @Autowired
    GoodsGiftRepository goodsGiftRepository;

    @Autowired
    GoodsRepository goodsRepository;

    @Autowired
    ShopInfoRepository shopInfoRepository;

    @Autowired
    ShoppingRepository shoppingRepository;

    public ApiResultBean queryAllUser(AntPage antPage, String nickname, String provinces, String city) {
        ApiResultBean apiResultBean = new ApiResultBean();
        Page<UserInfo> userInfos = userInfoRepository.findAllByDelFlagAndNicknameContainsAndProvincesContainsAndCityContainsOrderByCreateTime("0", nickname, provinces, city, antPage.getPageRequest());
        if (userInfos.getSize() > 0) {
            for (UserInfo userInfo : userInfos) {
                int a = goodsGiftRepository.countUserNum(userInfo.getOppenId(), "1", "0");
                userInfo.setGetNum(a);
            }
        }
        apiResultBean.setData(userInfos, antPage);
        apiResultBean.setCode(ApiResultBean.SUCCESS);
        apiResultBean.setMsg("查询成功");
        return apiResultBean;
    }

    public ApiResultBean userGetLog(String userId, AntPage antPage) {
        ApiResultBean apiResultBean = new ApiResultBean();

        if (StringUtil.isEmpty(userId)) {
            apiResultBean.setMsg("id不能为空");
            apiResultBean.setCode(ApiResultBean.FAIL);
            return apiResultBean;
        }
        UserInfo userInfo = userInfoRepository.findOne(userId);
        Page<GoodsGift> goodsGifts = goodsGiftRepository.findAllByUserOpenIdAndDelFlagAndState(userInfo.getOppenId(), "0", "1", antPage.getPageRequest());
        int a = goodsGiftRepository.countAllByDelFlagAndUserOpenIdAndState("0", userInfo.getOppenId(), "1");


        List<UserGetLogVo> userGetLogVos = new ArrayList<>();
        if (goodsGifts.getSize() > 0) {
            for (GoodsGift goodsGift : goodsGifts) {
                UserGetLogVo userGetLogVo = new UserGetLogVo();
                Goods goods = goodsRepository.findOne(goodsGift.getGoodsId());
                userGetLogVo.setGoodName(goods.getName());
                ShopInfo shopInfo = shopInfoRepository.findOne(goodsGift.getShopId());
                userGetLogVo.setShopName(shopInfo.getName());
                userGetLogVo.setGetTime(goodsGift.getGetTime());
                userGetLogVo.setGetNum(1);
                userGetLogVos.add(userGetLogVo);

            }

        }
        apiResultBean.setData(userGetLogVos, antPage, Long.valueOf(a));
        apiResultBean.setData(ApiResultBean.SUCCESS);
        apiResultBean.setMsg("查询成功");
        return apiResultBean;
    }


    public ApiResultBean userList(String salesmanId, AntPage antPage) {
        ApiResultBean apiResultBean = new ApiResultBean();
        if (StringUtil.isEmpty(salesmanId)) {
            apiResultBean.setCode(ApiResultBean.FAIL);
            apiResultBean.setMsg("参数有误");
            return apiResultBean;
        }
        //去重得到用户列表
        List<String> strings = goodsGiftRepository.findDistinctByUserOpenId("1", "0", salesmanId, (antPage.getPage() - 1) * antPage.getPageSize(), antPage.getPageSize());
        List<UserGetLogVo> userGetLogVos = new ArrayList<>();
        for (String str : strings) {
            UserGetLogVo userGetLogVo = new UserGetLogVo();
            List<UserInfo> userInfos = userInfoRepository.findAllByOppenIdAndDelFlag(str, "0");
            if (userInfos.size() > 0) {
                userGetLogVo.setUserImage(userInfos.get(0).getHeadimgurl());
            }
            userGetLogVo.setUserName(userInfos.get(0).getNickname());
            userGetLogVo.setUserId(userInfos.get(0).getId());
            int getNum = goodsGiftRepository.countAllByUserOpenIdAndDelFlagAndStateAndSalesmanId(str, "0", "1", salesmanId);
            userGetLogVo.setGetNum(getNum);
            userGetLogVos.add(userGetLogVo);
        }
        List<String> a = goodsGiftRepository.findAllDistinctByUserOpenId("1", "0", salesmanId);
        apiResultBean.setData(userGetLogVos, antPage, Long.valueOf(a.size()));
        apiResultBean.setCode(ApiResultBean.SUCCESS);
        apiResultBean.setMsg("查询成功");
        return apiResultBean;
    }

    public ApiResultBean userDetails(String salesmanId, String userId, AntPage antPage) {
        ApiResultBean apiResultBean = new ApiResultBean();
        if (StringUtil.isEmpty(salesmanId) || StringUtil.isEmpty(userId)) {
            apiResultBean.setMsg("参数不能为空");
            apiResultBean.setCode(ApiResultBean.FAIL);
            return apiResultBean;
        }
        UserInfo userInfo = userInfoRepository.findAllByIdAndDelFlag(userId, "0");
        Page<GoodsGift> goodsGifts = goodsGiftRepository.findAllBySalesmanIdAndUserOpenIdAndDelFlagAndState(salesmanId, userInfo.getOppenId(), "0", "1", antPage.getPageRequest());
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("images", userInfo.getHeadimgurl());
        hashMap.put("username", userInfo.getNickname());
        List<UserGetLogVo> userGetLogVos = new ArrayList<>();
        if (goodsGifts.getSize() > 0) {
            for (GoodsGift goodsGift : goodsGifts) {
                UserGetLogVo userGetLogVo = new UserGetLogVo();
                Shopping shopping = shoppingRepository.findAllByIdAndDelFlag(goodsGift.getShoppingId(), "0");
                    userGetLogVo.setShoppingName(shopping.getName());
                Goods goods = goodsRepository.findAllByIdAndDelFlag(goodsGift.getGoodsId(), "0");
                userGetLogVo.setGoodName(goods.getName());
                userGetLogVo.setGetTime(goodsGift.getGetTime());
                ShopInfo shopInfo = shopInfoRepository.findAllByDelFlagAndId("0", goodsGift.getShopId());
                userGetLogVo.setShopName(shopInfo.getName());
                userGetLogVos.add(userGetLogVo);

            }
        }
        apiResultBean.setData(hashMap);
        apiResultBean.setData(userGetLogVos, antPage, goodsGifts.getTotalElements());
        apiResultBean.setMsg("获取成功");
        apiResultBean.setCode(ApiResultBean.SUCCESS);
        return apiResultBean;
    }

}
